/************************************************************************************/
/*  Program name CAPGLIMP_AUTOPOST_SET.sql                                        	*/
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 25 JAN, 2006   NIP Karen         First created                                   */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPGLIMP_AUTOPOST_SET';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_GL_AUTO_POSTING_SETS;
	
	lv_Next_Seq_ID		NUMBER;
	
BEGIN
	UPDATE
		GL_AUTOMATIC_POSTING_SETS
	SET	
		AUTOPOST_SET_NAME	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(AUTOPOST_SET_NAME,1,4), AUTOPOST_SET_NAME)
	WHERE AUTOPOST_SET_ID IN
		(
		SELECT 
			AUTOPOST_SET_ID
		FROM 
			CRST_GL_AUTO_POSTING_SETS
		);
	
	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	GL_AUTOMATIC_POSTING_SETS_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT	INTO CRST_GL_AUTO_POSTING_SETS_MAP	VALUES
			(
			C1.AUTOPOST_SET_ID,
			C1.SET_OF_BOOKS_ID,
			CAPBRIMP_MAPPING.RenameStr(SUBSTR(C1.AUTOPOST_SET_NAME,1,4), C1.AUTOPOST_SET_NAME),
			lv_Next_Seq_ID,
			CAPBRIMP_MAPPING.GetSOBIDMapping(C1.SET_OF_BOOKS_ID),
			C1.AUTOPOST_SET_NAME
			);
			
			INSERT INTO GL_AUTOMATIC_POSTING_SETS
			VALUES
			(
			lv_Next_Seq_ID,
			CAPBRIMP_MAPPING.GetSOBIDMapping(C1.set_of_books_id),
			C1.autopost_set_name,
			C1.submit_all_priorities_flag,
			C1.enabled_flag,
			C1.last_update_date,
			CAPBRIMP_COMMON.GetUser(C1.last_updated_name),
			C1.creation_date,
			CAPBRIMP_COMMON.GetUser(C1.created_name),
			C1.last_update_login,
			C1.description,
			C1.num_of_priority_options,
			C1.effective_days_before,
			C1.effective_days_after,
			C1.attribute1,
			C1.attribute2,
			C1.attribute3,
			C1.attribute4,
			C1.attribute5,
			C1.attribute6,
			C1.attribute7,
			C1.attribute8,
			C1.attribute9,
			C1.attribute10,
			C1.attribute11,
			C1.attribute12,
			C1.attribute13,
			C1.attribute14,
			C1.attribute15,
			C1.context
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;
	
	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/